#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <hpdf.h>
#include <wchar.h>
#include <iostream>
#include <locale.h>
#include <ft2build.h>

#include FT_FREETYPE_H

#define PAGE_WIDTH 600
#define PAGE_HEIGHT 740
#define PAGE_MARGIN 50
#define CHAR_PER_ROW 10
#define CHARS_PER_PAGE 200
#define FONT_SIZE 25

// 错误处理函数
void error_handler(HPDF_STATUS error_no, HPDF_STATUS detail_no, void *user_data)
{
    printf("ERROR: %04X, %04X", (HPDF_UINT)error_no, (HPDF_UINT)detail_no);
}

int main()
{
    HPDF_Doc pdf;       // PDF文档对象
    HPDF_Page page;     // PDF页面对象
    HPDF_Font font;     // PDF字体对象
    HPDF_STATUS status; // PDF操作状态

    FT_Library library;
    FT_Face face;
    FT_UInt glyph_index;

    // 创建PDF文档
    pdf = HPDF_New(error_handler, NULL);
    if (!pdf)
    {
        printf("ERROR: Cannot create PDF document");
        return 1;
    }

    // 初始化 FreeType 库
    if (FT_Init_FreeType(&library))
    {
        // 错误处理
        std::cerr << "FT_Init_FreeType ERROR" << std::endl;
    }

    // 加载字体文件
    if (FT_New_Face(library, "/mnt/e/libharu_demo/resource/Arial_Unicode_MS.ttf", 0, &face))
    {
        // 错误处理
        std::cerr << "FT_New_Face ERROR" << std::endl;
    }

    // 添加新页面
    page = HPDF_AddPage(pdf);
    if (!page)
    {
        printf("ERROR: Cannot add a new page");
        HPDF_Free(pdf);
        return 1;
    }

    // 设置页面宽度和高度
    HPDF_Page_SetWidth(page, PAGE_WIDTH);
    HPDF_Page_SetHeight(page, PAGE_HEIGHT);

    // 使用UTF编码
    HPDF_UseUTFEncodings(pdf);
    const char *fontName = HPDF_LoadTTFontFromFile(pdf, "/mnt/e/libharu_demo/resource/Arial_Unicode_MS.ttf", HPDF_TRUE);
    font = HPDF_GetFont(pdf, fontName, "UTF-8");
    if (!font)
    {
        printf("ERROR: Cannot load font");
        HPDF_Free(pdf);
        return 1;
    }

    // 设置字体和大小
    HPDF_Page_SetFontAndSize(page, font, FONT_SIZE);

    // 设置本地化环境
    setlocale(LC_ALL, "");

    // 分配内存空间存储字符
    auto cstr = (char *)malloc(sizeof(wchar_t) * CHAR_PER_ROW * 100);
    if (cstr == NULL)
    {
        perror("malloc");
        return 1;
    }

    unsigned int total_chars = 0; // 记录总字符数
    for (unsigned int i = 0; i <= 65535; ++i)
    {
        if (!FT_Get_Char_Index(face, i))
            continue;

        wchar_t wbuf[2] = {(wchar_t)i, L'\0'};

        // 将宽字符转换为多字节字符
        if (std::wcstombs(cstr, wbuf, CHAR_PER_ROW + 1) == (size_t)-1)
        {
            perror("wcstombs");
            free(cstr);
            return 1;
        }

        // 输出文本到页面
        HPDF_Page_BeginText(page);
        HPDF_Page_TextOut(page, 80 + (total_chars % CHAR_PER_ROW) * (FONT_SIZE + 22),
                          PAGE_HEIGHT - PAGE_MARGIN - (total_chars / CHAR_PER_ROW + 1) * (FONT_SIZE + 5),
                          cstr);
        HPDF_Page_EndText(page);
        total_chars++;

        // 如果达到每页最大字符数，添加新页面并重置字符计数器
        if (total_chars >= CHARS_PER_PAGE)
        {
            page = HPDF_AddPage(pdf);
            if (!page)
            {
                printf("ERROR: Cannot add a new page");
                HPDF_Free(pdf);
                return 1;
            }
            HPDF_Page_SetWidth(page, PAGE_WIDTH);
            HPDF_Page_SetHeight(page, PAGE_HEIGHT);
            HPDF_Page_SetFontAndSize(page, font, FONT_SIZE);
            total_chars = 0;
        }
    }

    // 保存PDF文件
    status = HPDF_SaveToFile(pdf, "output.pdf");
    if (status != HPDF_OK)
    {
        printf("ERROR: Cannot save PDF file");
        HPDF_Free(pdf);
        return 1;
    }

    FT_Done_Face(face);
    FT_Done_FreeType(library);

    // 释放内存空间并关闭PDF文档
    free(cstr);
    HPDF_Free(pdf);
    printf("PDF created successfully\n");

    return 0;
}
